﻿/*******************************************************************************
 * Copyright © 2016 DJY 版权所有
 * Author: DJY
 * Description: 后台管理系统
 * Website：http://www.djyweb.cn
*********************************************************************************/
using DJY.Code;
using DJY.Service.WorkFlow;
using DJY.Domain.Entity.WorkFlow;
using DJY.Domain.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace DJY.Web.Areas.WorkFlow.Controllers
{
    public class WFCategoryController : ControllerBase
    {
        IWorkFlowEngine engine = OperatorProvider.Resolve<IWorkFlowEngine>();

        [HttpGet]
        
        public override ActionResult Index()
        {
            var user = OperatorProvider.GetCurrent();
            ViewBag.btns = engine.GetCateModuleButtons(user.UserId);
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeSelectJson()
        {
            string orgId = OperatorProvider.GetCurrent().CompanyId;
            var data = engine.CategoryList(new WFCategoryQueryInfo { fDeleteMark = false,orgId = orgId });
            var treeList = new List<TreeSelectModel>();
            foreach (WorkFlowCategory cate in data)
            {
                TreeSelectModel treeModel = new TreeSelectModel();
                treeModel.id = cate.id.ToString();
                treeModel.text = cate.name;
                treeModel.parentId = cate.parentId.ToString();
                treeModel.data = cate;
                treeList.Add(treeModel);
            }
            return Content(treeList.TreeSelectJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeJson()
        {
            string orgId = OperatorProvider.GetCurrent().CompanyId;
            var data = engine.CategoryList(new WFCategoryQueryInfo { fDeleteMark = false,orgId = orgId });
            var treeList = new List<TreeViewModel>();
            foreach (WorkFlowCategory cate in data)
            {
                TreeViewModel tree = new TreeViewModel();
                bool hasChildren = data.Any(t => t.parentId == cate.id);
                tree.id = cate.id.ToString();
                tree.text = cate.name.ToString();
                tree.value = cate.code;
                tree.parentId = cate.parentId.ToString();
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = data.Any(t => t.parentId == cate.id);
                treeList.Add(tree);
            }
            return Content(treeList.TreeViewJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeGridJson(string keyword)
        {
            WFCategoryQueryInfo queryInfo = new WFCategoryQueryInfo();
            queryInfo.orgId = OperatorProvider.GetCurrent().CompanyId;

            var data = engine.CategoryList(queryInfo);
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.TreeWhere(t => t.name.Contains(keyword) || t.code == keyword);
            }
            var treeList = new List<TreeGridModel>();
            foreach (WorkFlowCategory cate in data)
            {
                TreeGridModel treeModel = new TreeGridModel();
                bool hasChildren = data.Any(c => c.parentId == cate.id);
                treeModel.id = cate.id.ToString();
                treeModel.isLeaf = hasChildren;
                treeModel.parentId = cate.parentId.ToString();
                treeModel.expanded = hasChildren;
                treeModel.entityJson = cate.ToJson();
                treeList.Add(treeModel);
            }
            return Content(treeList.TreeGridJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            string orgId = OperatorProvider.GetCurrent().CompanyId;
            int id = 0;
            WorkFlowCategory cate = null;
            if (int.TryParse(keyValue, out id))
            {
                var data = engine.CategoryList(new WFCategoryQueryInfo { id = id,orgId = orgId });
                if(data != null && data.Count>0)
                {
                    cate = data[0];
                }
            }

            if(cate == null)
            {
                cate = new WorkFlowCategory();
            }
            return Content(cate.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(WorkFlowCategory cate)
        {
            var user = OperatorProvider.GetCurrent();
            cate.createUser = user.UserId;
            cate.lastUpdateUser = user.UserId;
            cate.orgId = user.CompanyId;
            EditItemResult result = engine.SaveCategory(cate);
            if(result.result)
            {
                return Success("操作成功。",null);
            }
            else
            {
                return Error(result.msg);
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string ids,bool state)
        {
            var user = OperatorProvider.GetCurrent();
            if (user == null)
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.Unauthorized);
            }

            if(string.IsNullOrEmpty(ids))
            {
                return Error("缺少必要参数");
            }

            List<string> idList = ids.Split(new char[] { ','},System.StringSplitOptions.RemoveEmptyEntries).ToList();
            EditItemResult result = engine.DeleteCategory(idList, state, user.UserId);
            if (result.result)
            {
                return Success("操作成功。", null);
            }
            else
            {
                return Error(result.msg);
            }
        }

        [HttpPost]
        [HandlerAjaxOnly]
        
        [ValidateAntiForgeryToken]
        public ActionResult Remove(string ids)
        {
            var user = OperatorProvider.GetCurrent();
            if (user == null)
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.Unauthorized);
            }

            if (string.IsNullOrEmpty(ids))
            {
                return Error("缺少必要参数");
            }

            List<string> idList = ids.Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries).ToList();
            EditItemResult result = engine.RemoveCategory(idList, user.UserId);
            if (result.result)
            {
                return Success("操作成功。",null);
            }
            else
            {
                return Error(result.msg);
            }
        }
    }
}
